//
// Created by yyancy on 2020/4/18.
//
#include <iostream>
#include <cstdlib>
#include "queue.h"
#include<string>
#include <memory>
#include <vector>

const int MIN_PER_HR = 60;

bool newcustomer(double x) {
    return (std::rand() * x / RAND_MAX < 1);
}


void remodel(std::string &str) {
    std::unique_ptr<std::string> ps(new std::string(str));
    str = *ps;
}

void printNum(int &n) {
    std::cout << n << std::endl;
}

int main() {
    using namespace std;

    vector<int> nums(0);
    nums.push_back(1);
    nums.push_back(2);
    nums.push_back(3);
    nums.push_back(4);
    nums.push_back(5);


    for (auto &n :nums) {
        std::cout << n << std::endl;
    }


    string s1(5, 65);

    string s3();
    cout << s1 << endl;

    std::srand(time(nullptr));

    cout << "Case Study:..";
    cout << "Enter maximum size of queue:";
    int qs;
    cin >> qs;

    Queue<Customer> line(qs);
    cout << "Enter the number of simulation hours: ";
    int hours;
    cin >> hours;

    long cyclelimit = MIN_PER_HR * hours;

    cout << "Enter the average number of customers per hour: ";
    double perhour;
    cin >> perhour;
    double min_per_cust;
    min_per_cust = MIN_PER_HR;


    Item temp;
    long turnaways = 0;
    long customers = 0;
    long served = 0;
    long sum_line = 0;
    int wait_time = 0;
    long line_wait = 0;

    for (int cycle = 0; cycle < cyclelimit; ++cycle) {
        if (newcustomer(min_per_cust)) {
            if (line.isfull())
                turnaways++;
            else {
                customers++;
                temp.set(cycle);
                line.enqueue(temp);
            }
        }
        if (wait_time <= 0 && !line.isempty()) {
            line.dequeue(temp);
            wait_time = temp.ptime();
            line_wait += cycle - temp.when();
            served++;
        }
        if (wait_time > 0)
            wait_time--;
        sum_line += line.queuecount();
    }

    if (customers > 0) {
        cout << "customers accepted: " << customers << endl;
        cout << "customers served: " << served << endl;
        cout << "customers turnaways: " << turnaways << endl;
        cout << "customers average queue size: ";
        cout.precision(2);
        cout.setf(ios_base::fixed, ios_base::floatfield);
        cout << (double) sum_line / cyclelimit << endl;
        cout << "average wait time"
             << (double) line_wait / served << "minutes" << endl;
    } else
        cout << "no customers!" << endl;

    cout << "Done!" << endl;
    return 0;
}